package org.pearl.springboot.minio.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.pearl.springboot.minio.pojo.query.User;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by TD on 2021/8/26
 */
@Controller
@RequestMapping("/mvc")
@Slf4j
public class MvcController {


    @GetMapping("/test")
    // @CrossOrigin(origins = {"*"})
    @ResponseBody
    public void test(HttpServletResponse response, HttpServletRequest request) throws IOException, InterruptedException {
        TimeUnit.MINUTES.sleep(10);
        User user=new User();
        user.setName("aaaaa");
        String result = JSON.toJSONString(user);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        String callback = request.getParameter("callback");
        result = callback + "(" + result + ")";
        response.getWriter().write(result);
    }



    @GetMapping("/testJson")
    @ResponseBody
    public void testJson(String name) {
        System.out.println("isInfoEnabled:" + log.isInfoEnabled());
        System.out.println("isDebugEnabled:" + log.isDebugEnabled());
        System.out.println("isErrorEnabled:" + log.isErrorEnabled());
        System.out.println("isTraceEnabled:" + log.isTraceEnabled());
        System.out.println("isWarnEnabled:" + log.isWarnEnabled());
        System.out.println(name);
    }


    @GetMapping("/testException")
    @ResponseBody
    public String testException() {
        int i = 4 / 0;
        return "user";
    }

    @PostMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody() {
        return "user";
    }

    @RequestMapping(value = {"/test", "/aaa", "/bbb"}, method = {RequestMethod.POST})
    public Object test() {
        return "test";
    }

    @RequestMapping(value = "/test001")
    public Object test001(@RequestParam(required = false, value = "name", defaultValue = "#{systemProperties['java.vm.version']}") String name, @RequestParam(required = true) int id) {
        return name + id;
    }

    @RequestMapping(value = "/test002"/*,produces="application/json",consumes="application/json"*/)
    public Object test002() {
        return "test";
    }


    @RequestMapping(value = "/test003/{name}/{id}")
    public Object test003(@PathVariable String name, @PathVariable Integer id) {
        return "test003";
    }

    @GetMapping("/getHeader")
    public String getRequestHeader(@RequestHeader("User-Agent") String agent) {
        System.out.println(agent);
        return "success";
    }

    @GetMapping("/getCookie")
    public String getCookieValue(@CookieValue(value = "JSESSIONID", required = false) String jId) {
        return jId;
    }


    @PostMapping("/testRequestBody")
    @ResponseBody
    public Object testRequestBody(@RequestBody User user) {
        return user;
    }


    @PostMapping("/testRequestEntity")

    public Object testRequestEntity(RequestEntity<User> requestEntity) {
        HttpMethod method = requestEntity.getMethod();
        URI url = requestEntity.getUrl();
        Type type = requestEntity.getType();
        User body = requestEntity.getBody();
        HttpHeaders headers = requestEntity.getHeaders();
        return body;
    }


    @GetMapping("/testResponseEntity")
    public ResponseEntity<User> testResponseEntity() {
        User user = new User();
        user.setName("吃个桃桃");
        user.setAge(15);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }


    @GetMapping("/testFast")
    @ResponseBody
    public List<User> testFast() {
        ArrayList<User> objects = Lists.newArrayList();
        return null;
    }
}
